package com.lanyam.travel.base.utils;

import java.io.File;
import java.io.IOException;

import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

public class FileUtil {
	/**
	 * 删除文件夹
	 * @param folderPath
	 */
	public static void delFolder(String folderPath) {  
	     try {  
	        delAllFile(folderPath); //删除完里面所有内容  
	        String filePath = folderPath;  
	        filePath = filePath.toString();  
	        File myFilePath = new File(filePath);  
	        myFilePath.delete(); //删除空文件夹  
	     } catch (Exception e) {  
	       e.printStackTrace();   
	     }  
	}  
	/**
	 * 删除指定文件夹下的所有文件
	 * @param path
	 * @return
	 */
	public static boolean delAllFile(String path) {  
       boolean flag = false;  
       File file = new File(path);  
       if (!file.exists())
       {  
         return flag;  
       }  
       if (!file.isDirectory())
       {  
         return flag;  
       }  
       String[] tempList = file.list();  
       File temp = null;  
       for (int i = 0; i < tempList.length; i++) 
       {  
          if (path.endsWith(File.separator))
          {  
             temp = new File(path + tempList[i]);  
          } 
          else 
          {  
              temp = new File(path + File.separator + tempList[i]);  
          }  
          if (temp.isFile()) 
          {  
             temp.delete();  
          }  
          if (temp.isDirectory()) 
          {  
             delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件  
             delFolder(path + "/" + tempList[i]);//再删除空文件夹  
             flag = true;  
          }  
       }  
       return flag;  
	} 
	/**
	 * 上传文件到指定文件夹
	 * @param files 上传的文件数组
	 * @param dbPathUri 保存目录的相对路径"/img/scenic/"+id || "/img/pic/"+id
	 * @param fileName 文件的名称
	 * @return 返回保存到数据库中的相对路径
	 */
	public static String uploadFile(MultipartFile[] files,String dbPathUri,String fileName)
	{
		if(null == files || StringUtils.isEmpty(dbPathUri) || StringUtils.isEmpty(fileName))
		{
			return null;
		}
		StringBuilder builder = new StringBuilder();
		if(files.length > 0)
		{
			String baseUri = Constant.FILE_UPLOAD_BASE_URI;
			MultipartFile originFile;
			for (int i = 0; i < files.length; i++) {
				originFile = files[i];
//				String fileName = originFile.getOriginalFilename();
				File targetFile = new File(baseUri+dbPathUri+"/"+fileName);
				if(!targetFile.getParentFile().exists())
				{
					targetFile.getParentFile().mkdirs();
				}
				try {
					if(targetFile.exists())
					{
						targetFile.delete();
					}
					originFile.transferTo(targetFile);
					builder.append(dbPathUri + "/" + fileName+";");
				} catch (IllegalStateException | IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		String imgUri = builder.toString();
		if(imgUri.length() > 0)
		{
			imgUri = imgUri.substring(0, imgUri.length() - 1);
		}
		return imgUri;
	}
	/**
	 * 上传文件到指定文件夹
	 * @param files 上传的文件数组
	 * @param dbPathUri 保存目录的相对路径"/img/scenic/"+id || "/img/pic/"+id
	 * @return 返回保存到数据库中的相对路径
	 */
	public static String uploadFile(MultipartFile[] files,String dbPathUri)
	{
		if(null == files || StringUtils.isEmpty(dbPathUri))
		{
			return null;
		}
		StringBuilder builder = new StringBuilder();
		if(files.length > 0)
		{
			String baseUri = Constant.FILE_UPLOAD_BASE_URI;
			MultipartFile originFile;
			for (int i = 0; i < files.length; i++) {
				originFile = files[i];
				String fileName = originFile.getOriginalFilename();
				File targetFile = new File(baseUri+dbPathUri+"/"+fileName);
				if(!targetFile.getParentFile().exists())
				{
					targetFile.getParentFile().mkdirs();
				}
				try {
					if(targetFile.exists())
					{
						targetFile.delete();
					}
					originFile.transferTo(targetFile);
					builder.append(dbPathUri + "/" + fileName+";");
				} catch (IllegalStateException | IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		String imgUri = builder.toString();
		if(imgUri.length() > 0)
		{
			imgUri = imgUri.substring(0, imgUri.length() - 1);
		}
		return imgUri;
	}
	/**
	 * 上传文件到指定文件夹
	 * @param files 上传的文件数组
	 * @param dbPathUri 保存目录的相对路径"/img/scenic/"+id || "/img/pic/"+id
	 * @return 返回保存到数据库中的相对路径
	 */
	public static String uploadFile(MultipartFile originFile,String dbPathUri)
	{
		if(null == originFile || StringUtils.isEmpty(dbPathUri))
		{
			return null;
		}
		StringBuilder builder = new StringBuilder();
		String baseUri = Constant.FILE_UPLOAD_BASE_URI;
		String fileName = originFile.getOriginalFilename();
		File targetFile = new File(baseUri+dbPathUri+"/"+fileName);
		if(!targetFile.getParentFile().exists())
		{
			targetFile.getParentFile().mkdirs();
		}
		try {
			if(targetFile.exists())
			{
				targetFile.delete();
			}
			originFile.transferTo(targetFile);
			builder.append(dbPathUri + "/" + fileName+";");
		} catch (IllegalStateException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String imgUri = builder.toString();
		if(imgUri.length() > 0)
		{
			imgUri = imgUri.substring(0, imgUri.length() - 1);
		}
		return imgUri;
	}
	/**
	 * 上传文件到指定文件夹
	 * @param files 上传的文件数组
	 * @param dbPathUri 保存目录的相对路径"/img/scenic/"+id || "/img/pic/"+id
	 * @param fileName 文件的名称
	 * @return 返回保存到数据库中的相对路径
	 */
	public static String uploadFile(MultipartFile originFile,String dbPathUri,String fileName)
	{
		if(null == originFile || StringUtils.isEmpty(dbPathUri) || StringUtils.isEmpty(fileName))
		{
			return null;
		}
		StringBuilder builder = new StringBuilder();
		String baseUri = Constant.FILE_UPLOAD_BASE_URI;
		File targetFile = new File(baseUri+dbPathUri+"/"+fileName);
		if(!targetFile.getParentFile().exists())
		{
			targetFile.getParentFile().mkdirs();
		}
		try {
			if(targetFile.exists())
			{
				targetFile.delete();
			}
			originFile.transferTo(targetFile);
			builder.append(dbPathUri + "/" + fileName+";");
		} catch (IllegalStateException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String imgUri = builder.toString();
		if(imgUri.length() > 0)
		{
			imgUri = imgUri.substring(0, imgUri.length() - 1);
		}
		return imgUri;
	}
}
